//
//  XDVideoPlayerViewController.m
//  testDownloadDemo
//
//  Created by XDWY on 2018/3/21.
//  Copyright © 2018年 XDWY. All rights reserved.
//

#import "XDVideoPlayerViewController.h"
#import "SelVideoPlayer.h"
#import "SelPlayerConfiguration.h"
#import "AppDelegate.h"

#import "HSDownloadManager.h"

// 网络视频保存本地后播放
#define kVideoUrlLocal @"http://120.25.226.186:32812/resources/videos/minion_03.mp4"


@interface XDVideoPlayerViewController ()

@property (nonatomic, strong) SelVideoPlayer *player;

// 下载的进度条
@property (nonatomic,strong)UIProgressView *progressView;

// 下载的进度数字
@property(nonatomic,strong)UILabel *progressLable;
// 开始下载按钮
@property(nonatomic,strong)UIButton *startBtn;
// 删除下载的文件
@property(nonatomic,strong)UIButton *deleteBtn;


@end

@implementation XDVideoPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] > 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    SelPlayerConfiguration *configuration = [[SelPlayerConfiguration alloc]init];
    configuration.shouldAutoPlay = YES;
    configuration.supportedDoubleTap = YES;
    configuration.shouldAutorotate = YES;
    configuration.repeatPlay = YES;
    configuration.statusBarHideState = SelStatusBarHideStateFollowControls;
    
    if (self.isLocal) {
        configuration.sourceUrl = [NSURL fileURLWithPath:self.playUrl];
    }else {
        configuration.sourceUrl = [NSURL URLWithString:self.playUrl];
    }
    
    configuration.videoGravity = SelVideoGravityResizeAspect;
    
    CGFloat width = self.view.frame.size.width;
    _player = [[SelVideoPlayer alloc]initWithFrame:CGRectMake(0, 100, width, 300) configuration:configuration];
    [self.view addSubview:_player];
    
    CGFloat progressSuperY = CGRectGetMaxY(_player.frame) +10;
    self.progressView.frame =CGRectMake(10,progressSuperY +14, 160, 2);
    self.progressLable.frame =CGRectMake(CGRectGetMaxX(self.progressView.frame) +10, progressSuperY, 40, 30);
    self.startBtn.frame =CGRectMake(CGRectGetMaxX(self.progressLable.frame) +10, progressSuperY, 45, 30);
    self.deleteBtn.frame =CGRectMake(CGRectGetMaxX(self.startBtn.frame) +5, progressSuperY, 45, 30);
    
    [self.view addSubview:self.progressView];
    [self.view addSubview:self.progressLable];
    [self.view addSubview:self.startBtn];
    [self.view addSubview:self.deleteBtn];

    
    // Do any additional setup after loading the view.
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_player _deallocPlayer];
}


- (NSString *)getTitleWithDownloadState:(DownloadState)state
{
    switch (state) {
        case DownloadStateStart:
            return @"暂停";
        case DownloadStateSuspended:
        case DownloadStateFailed:
            return @"开始";
        case DownloadStateCompleted:
            return @"完成";
        default:
            break;
    }
}

#pragma mark - Action Methods
-(void)startBtnClick:(UIButton *)btn {
    
    WS(ws);
    [[HSDownloadManager sharedInstance]download:kVideoUrlLocal progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            ws.progressLable.text = [NSString stringWithFormat:@"%.f%%", progress * 100];
            ws.progressView.progress = progress;
        });
    } state:^(DownloadState state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [ws.startBtn setTitle:[self getTitleWithDownloadState:state] forState:UIControlStateNormal];
        });
    }];

}



-(void)deleteBtnClick:(UIButton *)btn {
    [[HSDownloadManager sharedInstance] deleteFile:kVideoUrlLocal];
    
    self.progressLable.text = [NSString stringWithFormat:@"%.f%%", [[HSDownloadManager sharedInstance] progress:kVideoUrlLocal] * 100];
    self.progressView.progress = [[HSDownloadManager sharedInstance] progress:kVideoUrlLocal];
    [self.startBtn setTitle:[self getTitleWithDownloadState:DownloadStateSuspended] forState:UIControlStateNormal];
}
#pragma mark - Setter & Getter
-(UIProgressView *)progressView {
    if (!_progressView) {
        _progressView =[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
        _progressView.backgroundColor =[UIColor blueColor];
    }
    return _progressView;
}

-(UILabel *)progressLable {
    if (!_progressLable) {
        _progressLable =[[UILabel alloc]init];
//        _progressLable.backgroundColor =[UIColor orangeColor];
        _progressLable.text =@"0%";
        _progressLable.font =[UIFont systemFontOfSize:15];
    }
    return _progressLable;
}
-(UIButton *)startBtn {
    if (!_startBtn) {
        _startBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_startBtn setTitle:@"开始" forState:UIControlStateNormal];
        _startBtn.backgroundColor =[UIColor redColor];
        _startBtn.titleLabel.font =[UIFont systemFontOfSize:15];
        [_startBtn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _startBtn;
}
-(UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        _deleteBtn.backgroundColor =[UIColor redColor];
        _deleteBtn.titleLabel.font =[UIFont systemFontOfSize:15];
        [_deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
